import 'package:fish_redux/fish_redux.dart';

import '../../../app_files.dart';
import 'action.dart';
import 'state.dart';

Effect<InstallTypeEditViewState> buildEffect() {
  return combineEffects(<Object, Effect<InstallTypeEditViewState>>{
    InstallTypeEditViewAction.onTap: _onTap,
    InstallTypeEditViewAction.onSubmit: _onSubmit,
  });
}

void _onSubmit(Action action, Context<InstallTypeEditViewState> ctx) async {
  CommonUtils.showWaitLoading(ctx.context, descText: '正在提交中');
  ResultData res =
      await CommonNetWork.engineerEditInstallType(ctx.state.installType,ctx.state.itemID);
  Navigator.pop(ctx.context);
  if (res.success) {
    bus.emit('refreshList');
    await CommonUtils.showAlertView(ctx.context, '提交成功');
    Navigator.pop(ctx.context, ctx.state.installType);
  } else {
    CommonUtils.showErrorInfo(ctx.context, res.desc);
  }
}

void _onTap(Action action, Context<InstallTypeEditViewState> ctx) async {
  List<String> values = [];
  if (ctx.state.installType.isNotEmpty) {
    values = ctx.state.installType.split(',');
  }
  if (values.contains(action.payload)) {
    values.remove(action.payload);
  }
  else {
    values.add(action.payload);
  }
  ctx.state.installType = values.join(',');
  ctx.dispatch(InstallTypeEditViewActionCreator.onAction());
}
